package com.xzy.rabbit.demo.exchange.b;

import com.rabbitmq.client.Connection;
import com.xzy.rabbit.common.util.RabbitUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Direct交换机（路由模式）
 *
 * @author xzy
 * @date 2021/9/2710:39
 */
class Main {
    private static final String EXCHANGE_NAME = "xzy.exchange.direct";
    private static final Integer MESSAGE_COUNT = 3;
    private static final long WORK_TIME = 3000L;

    private static final String QUEUE_NAME_1 = "xzy.queue.direct1";
    private static final String QUEUE_NAME_2 = "xzy.queue.direct2";
    private static final String[] ROUTING_KEY_1 = {"DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
    private static final String[] ROUTING_KEY_2 = {"ERROR", "FATAL"};


    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 1.获取连接
        Connection connection = RabbitUtil.getConnection();

        // 2.发布消息
        Thread producer = new Thread(new Producer(connection, EXCHANGE_NAME, ROUTING_KEY_1, MESSAGE_COUNT, WORK_TIME), "PRODUCER");

        // 2.订阅消息
        Thread logConsumer = new Thread(new Consumer(connection, EXCHANGE_NAME, QUEUE_NAME_1, ROUTING_KEY_1), "CONSUMER_LOG");
        Thread diskConsumer = new Thread(new Consumer(connection, EXCHANGE_NAME, QUEUE_NAME_2, ROUTING_KEY_2), "CONSUMER_DIS");

        producer.start();
        logConsumer.start();
        diskConsumer.start();

        producer.join();
        logConsumer.join();
        diskConsumer.join();
    }
}
